package kr.saeildamil.androidstudy_second.actionbar;

import kr.saeildamil.andrordstudy.second.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ShareActionProvider;
import android.widget.TextView;

public class ShareAction extends Activity {
	EditText mEdit;
	TextView mText;
	ShareActionProvider provider;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);

		mText = new TextView(this);
		mText.setText("공유 액션 프로파이더를 테스트");

		EditText mEdit = new EditText(this);
		mEdit.setText("예습 복습 철저");

		LayoutParams params = new LayoutParams(new LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));

		layout.addView(mText);
		layout.addView(mEdit);
		setContentView(layout);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.shareactionmenu, menu);

		MenuItem share = menu.findItem(R.id.share);
		ShareActionProvider provider = (ShareActionProvider) share
				.getActionProvider();
		provider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
		provider.setShareIntent(createShareIntent());

		MenuItem sharemenu = menu.findItem(R.id.sharemenu);
		ShareActionProvider providermenu = (ShareActionProvider) share
				.getActionProvider();
		providermenu
				.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
		providermenu.setShareIntent(createShareIntent());

		return super.onCreateOptionsMenu(menu);
	}

	private Intent createShareIntent() {
		Intent shareIntent = new Intent(Intent.ACTION_SEND);
		shareIntent.setType("text/plain");
		shareIntent.putExtra(Intent.EXTRA_TEXT, mText.getText().toString());

		startActivity(Intent.createChooser(shareIntent, "aa"));
		return shareIntent;
	}

	private void setShareIntent(Intent shareIntent) {
		if (provider != null) {
			provider.setShareIntent(shareIntent);
		}
	}

}
